package
{
	import feathers.controls.List;
	import feathers.data.ListCollection;
	import flash.desktop.NativeApplication;
	import starling.core.Starling;
	import starling.display.Button;
	import starling.display.Image;
	import starling.events.Event;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	import starling.extensions.PDParticleSystem;
	import starling.textures.Texture;
	import flash.media.SoundChannel;
	import flash.events.EventDispatcher;
	public class SeasonSelectScreen extends UIScreen
	{
		private var _back:Button;
		//private var theme:MetalWorksMobileTheme;
		private var mTickerTape:List;
		private var _channel:SoundChannel;
		public function SeasonSelectScreen()
		{
			super();
			var SeasonSelectpxConfig:XML = XML(new Assets.SeasonSelectpx());
			var SeasonSelectTexture:Texture = Texture.fromBitmap(new Assets.SeasonSelect());
			
			var ss:PDParticleSystem =new PDParticleSystem(SeasonSelectpxConfig, SeasonSelectTexture);
			ss.x = 160;
			ss.y = 240;
			
			
			_back=new Button (Assets.getUIScreens().getTexture("arrowleft"));
			addChild(new Image(Assets.getUIScreens().getTexture("seasonselect")));
			addChild(ss);
			addChild(_back);
			
			Starling.juggler.add(ss);
			
			ss.start();
			//ss.start(9999);
			
			// ticker tape
			mTickerTape = new List();
			this.addChild( mTickerTape );
			
			var seasonsList:ListCollection = new ListCollection(
				[					
					{ thumbnail: Assets.getUIScreens().getTexture("selectHalloweenBtn") },
					{ thumbnail: Assets.getUIScreens().getTexture("selectFlowerBtn") },
					{ thumbnail: Assets.getUIScreens().getTexture("selectFishBtn") },
					{ thumbnail: Assets.getUIScreens().getTexture("selectcomingsoon") },
				]);
			
			mTickerTape.dataProvider = seasonsList;
			mTickerTape.itemRendererProperties.iconSourceField = "thumbnail";
			mTickerTape.itemRendererProperties.height = 200;			
			mTickerTape.height = Starling.current.stage.stageHeight;
			mTickerTape.width = ((Texture)(seasonsList.getItemAt(0).thumbnail)).width;
			mTickerTape.pivotX = mTickerTape.width;
			mTickerTape.x = Starling.current.stage.stageWidth * 0.9;		
			mTickerTape.addEventListener( Event.CHANGE, list_changeHandler );			
		}
		
		private function list_changeHandler( event:Event ):void
		{
			//mTickerTape.removeEventListener( Event.CHANGE, list_changeHandler );
			var list:List = List( event.currentTarget );
			if(list.selectedIndex != -1 && list.selectedIndex < 3)
			{								
				dispatchEvent(new UINavigateEvent(UINavigateEvent.UI_GOTO, true, {season:list.selectedIndex, screenID:"LevelSelect"}));
				list.selectedItem = null;
				list.selectedIndex = -1;
			}				
			
			//trace( "selectedIndex:", list.selectedIndex );
		}			
		
		protected override function onAddedToStage(event:Event):void
		{
			super.onAddedToStage(event);								
			NativeApplication.nativeApplication.addEventListener(flash.events.Event.DEACTIVATE , handleDeactivate, false, 0, true);
			//this.theme = new MetalWorksMobileTheme(this.stage);			
			
			var ht:Number = Starling.current.stage.stageHeight * 0.005;
			var wid:Number = Starling.current.stage.stageWidth * 0.003;				
			
			_back.y= Starling.current.stage.stageHeight * 0.85;							
			_back.addEventListener(starling.events.TouchEvent.TOUCH, onTouchedBack);
		}		
		
		private function handleDeactivate(event:flash.events.Event):void 
		{
			//the app is now losing focus
			//musicChannel.stop();
			_channel.stop();
		}
		
		private function onTouchedBack(e:TouchEvent):void
		{						
			var touch:Touch = e.getTouch(stage);			
			
			if(touch.phase == TouchPhase.ENDED)
			{				
				dispatchEvent(new UINavigateEvent(UINavigateEvent.UI_BACK, true));
			}
		}
		public override function onEnter():void
		{
			_channel = Assets.SoundStore.play(0,999);
		}
		
		public override function onExit():void
		{
			_channel.stop();
		}
	}
}